package me.ereach.myapi.controller;

import com.google.gson.Gson;
import me.ereach.myapi.mapper.ProductsMapper;
import me.ereach.myapi.model.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api")
public class ProductsController {
    @Autowired
    private ProductsMapper productsMapper;

    @PutMapping("/products")
    public String putProducts(
        @RequestParam("productId") Integer productId,
        @RequestParam("name") String name,
        @RequestParam("quantityInStock") Integer quantityInStock,
        @RequestParam("unitPrice") Float unitPrice
    ) {
        Products products = new Products();
        products.setProductId(productId);
        products.setName(name);
        products.setQuantityInStock(quantityInStock);
        products.setUnitPrice(unitPrice);

        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("rowCount", productsMapper.updateById(products));

        Gson gson = new Gson();
        String jsonStr = gson.toJson(myMap);

        return jsonStr;
    }

    @DeleteMapping("/products")
    public String deleteProducts(
        @RequestParam("productId") Integer productId
    ) {
        Products products = new Products();
        products.setProductId(productId);

        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("rowCount", productsMapper.deleteById(products));

        Gson gson = new Gson();
        String jsonStr = gson.toJson(myMap);

        return jsonStr;
    }

    @PostMapping("/products")
    public String postProducts(
        @RequestParam("name") String name,
        @RequestParam("quantityInStock") Integer quantityInStock,
        @RequestParam("unitPrice") Float unitPrice
    ) {
        Products products = new Products();

        products.setName(name);
        products.setQuantityInStock(quantityInStock);
        products.setUnitPrice(unitPrice);

        Integer retvar = productsMapper.insert(products);

        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("productId", products.getProductId());
        myMap.put("rowCount", retvar);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(myMap);

        return jsonStr;
    }

    @GetMapping("/products")
    public List<Products> getProducts() {
        return productsMapper.selectList(null);
    }

    // products/2 query products where id = 2
    // products/bac query products where name contains 'ban'
    @GetMapping("/products/{name}")
    public List<Products> getProductsByName(@PathVariable("name") String name) {
        try {
            Integer id = Integer.parseInt(name);
            List<Products> lst = new ArrayList<Products>();
            Products p = productsMapper.selectById(id);
            if (!p.equals(null)) lst.add(p);
            return lst;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            return productsMapper.getProductsByName(name);
        }
    }
}
